package com.mimosa.common.system.menu;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.mimosa.util.config.YesNoType;

public class MenuItemHandler extends DefaultHandler {
	/**
	 * 菜单信息列表
	 */
	private List<TMenuItem> menuItemList = null;
	static final String MENUITEM = "menuItem";
	private List<String> pathInfo = new ArrayList<String>();
	private Map<String, TMenuItem> context = new HashMap<String, TMenuItem>();

	public MenuItemHandler() {
		this.menuItemList = new ArrayList<TMenuItem>();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if (qName.endsWith(MENUITEM)) {
			TMenuItem item = new TMenuItem();
			item.setMenuCode(attributes.getValue("code"));
			String name = attributes.getValue("name");
			item.setMenuName(name);
			String needAuth = attributes.getValue("needAuth");
			this.pathInfo.add(name);
			String url = attributes.getValue("url");
			if (StringUtils.isNotBlank(needAuth)) {
				if (Boolean.valueOf(needAuth)) {
					item.setNeedAuth(YesNoType.Y.name());
				} else {
					item.setNeedAuth(YesNoType.N.name());
				}
			} else {
				item.setNeedAuth(this.getParentAuthInfo(item.getMenuCode()));
			}
			if (StringUtils.isBlank(url)) {
				url = StringUtils.EMPTY;
			}
			String desc = attributes.getValue("desc");
			item.setMenuDesc(desc);
			// 是否需要授权才可以访问
			String needGrant = attributes.getValue("needGrant");
			if (StringUtils.isNotBlank(needGrant)) {
				if (Boolean.valueOf(needGrant)) {
					item.setNeedGrant(YesNoType.Y.name());
				} else {
					item.setNeedGrant(YesNoType.N.name());
				}
			} else {
				item.setNeedGrant(this.getParentAuthGrant(item.getMenuCode()));
			}
			item.setMenuUrl(url);
			// 设置一些默认值
			item.setLastUpdateTime(new Date());
			item.setMenuIcon("menuIcon");
			item.setMenuPath(StringUtils.join(this.pathInfo, "&nbsp;&gt;&nbsp;"));
			item.setState(YesNoType.Y.name());
			// 设置子节点信息
			TMenuItem parent = this.context.get(item.getMenuCode().substring(0, item.getMenuCode().length() - MenuItemHelper.ID_LENGTH));
			if (parent != null) {
				parent.setChildNum(parent.getChildNum() + 1);
			}
			this.menuItemList.add(item);
			this.context.put(item.getMenuCode(), item);
		}
	}

	String getParentAuthInfo(String currentId) {
		String result = YesNoType.N.name();
		TMenuItem menuInfo = this.context.get(currentId.substring(0, currentId.length() - MenuItemHelper.ID_LENGTH));
		if (menuInfo != null) {
			result = menuInfo.getNeedAuth();
		}
		return result;
	}

	String getParentAuthGrant(String currentId) {
		String result = YesNoType.N.name();
		TMenuItem menuInfo = this.context.get(currentId.substring(0, currentId.length() - MenuItemHelper.ID_LENGTH));
		if (menuInfo != null) {
			result = menuInfo.getNeedGrant();
		}
		return result;
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (qName.endsWith(MENUITEM)) {
			this.pathInfo.remove(this.pathInfo.size() - 1);
		}
	}

	public List<TMenuItem> getMenuItemList() {
		return menuItemList;
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
	}
}